<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Cookie;

/**
 * By：wl
 * Date：2018.11
 * Function:登录
 */

class Admin 
{ 
    /*登录*/
    public function login()
    {
    	return view();
    }
    /* 获取验证码*/
    public function vertify()
    {
        return captcha_src();
    }
    /* 登陆验证*/
    public function check()
    {
        $username = input('username');
        $password = input('password');
        // $vercode = input('vercode');

        //1、验证码
        /*if(!captcha_check($vercode)){
            //验证码错误
            echojson('验证码错误',0);
        }else{*/
            //验证码正确
            
            //2、验证账号
            $res = Db::table('adminuser')->where("username",$username)->where("password",MD5($password))->find();
            
            if($res){
                //账号验证
                if($res['status'] != 1){
                    echojson(false,0,'管理员账号已被锁定');
                }
                //角色验证
                $res2 = Db::table('admingroup')->where("id",$res['groupid'])->find();
                if($res2['status'] !=1 ){
                    echojson(false,0,'角色已被锁定');
                }

                //3、成功后加SESSION，加cookie
                Session::set('admin_userinfo_tp5'.$res['id'],$res);
                Cookie::set('admin_userid_tp5',$res['id'],60*60*6);
                $now = time();
                Db::execute("update adminuser set lastlogin = $now where id = $res[id]");
                echojson(true,1,'登录成功');
            }else{
                echojson(false,0,'账号或密码错误');
            }
        // }
    }
    /* 获取验证码*/
    public function icon()
    {
        return view();
    }
    
}
